package cn.xspace.wnx.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.util.DateUtils;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.List;

import cn.xspace.library.util.MsgType;
import cn.xspace.wnx.R;
import cn.xspace.chatui.bean.MessageEntity;
import cn.xspace.library.bean.Type;

/**
 * 群组助手的Adapter
 * Created by Wubj on 2016/12/15 0015.
 */
public class GroupManagerAdapter extends BaseAdapter {

    private static final String TAG = GroupManagerAdapter.class.getSimpleName();

    private Context mContext;
    private List<MessageEntity> dataList;

    public GroupManagerAdapter(Context context, List<MessageEntity> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return Long.parseLong(TextUtils.isEmpty(dataList.get(position).inserttime) ? "0" : dataList.get(position)
                .inserttime);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GroupHolder viewHolder;
        MessageEntity messageEntity = dataList.get(position);
        if (convertView == null) {
            viewHolder = new GroupHolder();
            convertView = View.inflate(mContext, R.layout.list_item_message, null);
            viewHolder.findView(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupHolder) convertView.getTag();
        }
        viewHolder.setData(viewHolder, messageEntity);
        return convertView;
    }

    private class GroupHolder {

        ImageView logoIv;
        TextView unreadTv;
        TextView titleTv;
        TextView contentTv;
        TextView timeTv;
        ImageView excuseIv;
        ImageView statusIv;
        RelativeLayout relative_bg;

        public void findView(View v) {
            this.logoIv = (RoundAngleImageView) v.findViewById(R.id.imageview_head);
            this.unreadTv = (TextView) v.findViewById(R.id.tv_unreadcount);
            this.titleTv = (TextView) v.findViewById(R.id.tv_title);
            this.contentTv = (TextView) v.findViewById(R.id.tv_content);
            this.timeTv = (TextView) v.findViewById(R.id.tv_time);
            this.excuseIv = (ImageView) v.findViewById(R.id.imageview_excuse);
            this.statusIv = (ImageView) v.findViewById(R.id.msg_status);
            this.relative_bg = (RelativeLayout) v.findViewById(R.id.relative_bg);
        }

        public void setData(GroupHolder viewHolder, MessageEntity messageEntity) {

            if (messageEntity.group_type == Type.GroupType.ORGANIZATION) {
                viewHolder.logoIv.setImageResource(R.drawable.icon_default_org);
            } else if (messageEntity.group_type == Type.GroupType.PROFESSIONAL) {
                viewHolder.logoIv.setImageResource(R.drawable.icon_default_profess);
            } else {
                //显示头像
                if (!TextUtils.isEmpty(messageEntity.msg_logo)) {
                    LogUtil.d(TAG, "wubaojie>>>setData:msg_logo: " + messageEntity.msg_logo);
                    Picasso.with(mContext)
                            .load(messageEntity.msg_logo)
                            .placeholder(R.drawable.icon_header_default)
                            .error(R.drawable.icon_header_default)
                            .into(viewHolder.logoIv);
                }
            }


            //显示未读数字
            if (messageEntity.msg_unread_count == 0) {
                viewHolder.unreadTv.setVisibility(View.GONE);
            } else {
                if (messageEntity.msg_warn_status == MessageEntity.WarnStatus.NOT_WARN.toNumber()) {
                    viewHolder.unreadTv.setVisibility(View.VISIBLE);
                    viewHolder.unreadTv.setText("");
                    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(viewHolder.unreadTv
                            .getLayoutParams());
                    layoutParams.width = 30;
                    layoutParams.height = 30;
                    layoutParams.gravity = Gravity.TOP | Gravity.RIGHT;
                    viewHolder.unreadTv.setLayoutParams(layoutParams);
                } else {
                    viewHolder.unreadTv.setVisibility(View.VISIBLE);
                    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-2, -2); //wrap_content
                    layoutParams.gravity = Gravity.TOP | Gravity.RIGHT;
                    viewHolder.unreadTv.setLayoutParams(layoutParams);
                    if (messageEntity.msg_unread_count > 99) {
                        viewHolder.unreadTv.setText("...");
                    } else {
                        viewHolder.unreadTv.setText(String.valueOf(messageEntity.msg_unread_count));
                    }
                }
            }
            if (!TextUtils.isEmpty(messageEntity.msg_last_time)) {
                //最新时间
                viewHolder.timeTv.setText(DateUtils.getMessageList(messageEntity.msg_last_time, DateUtils.FORMAT_1));
            }
            //消息打扰状态
            if (messageEntity.msg_warn_status == MessageEntity.WarnStatus.NOT_WARN.toNumber()) {
                viewHolder.excuseIv.setVisibility(View.VISIBLE);
            } else {
                viewHolder.excuseIv.setVisibility(View.INVISIBLE);
            }
            if (messageEntity.msg_type == MsgType.CHAT_CROUP.toNumber()) {
                //群聊
                //标题显示
                viewHolder.titleTv.setText(TextUtils.isEmpty(messageEntity.msg_group_name) ? "未命名" : messageEntity
                        .msg_group_name);
                //内容显示
                if (messageEntity.msg_send_status == MessageEntity.MsgSendStatus.FAILED.toNumber()) {
                    //自己发送不成功
                    viewHolder.statusIv.setVisibility(View.VISIBLE);
                    viewHolder.contentTv.setText(messageEntity.msg_content);
                } else {
                    viewHolder.statusIv.setVisibility(View.GONE);
                    viewHolder.contentTv.setText(messageEntity.msg_content);
                }
            }
            //置顶状态
            LogUtil.d("messageEntity.msg_index>>" + messageEntity.msg_index);
            if (messageEntity.msg_index != 0) {
                viewHolder.relative_bg.setBackgroundColor(mContext.getResources().getColor(R.color.bg_msg_item_top));
            } else {
                viewHolder.relative_bg.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            }
        }
    }

}
